/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2016-01-26 10:10:46
 * Last Modified: 2017-07-31 14:39:02
 * File Name: my_appkey.go
 * Description:
 */

package auth

import (
//"sync"
)

var (
	authAppKey *authAppKeyService
)

//每个appkey的基本信息
type AppKeyItem struct {
	Desc      string //appkey的描述
	SecretKey string //私钥
}

func newAppKeyItem(desc, secrect string) *AppKeyItem {
	return &AppKeyItem{
		Desc:      desc,
		SecretKey: secrect,
	}
}

type authAppKeyService struct {
	appKeyInfo map[string]*AppKeyItem
	//appkeyInfoLock *sync.RWMutex
}

/*
	构造authAppKeyService实例
*/
func newAuthAppKeyService() *authAppKeyService {
	service := &authAppKeyService{
		appKeyInfo: make(map[string]*AppKeyItem),
		//appkeyInfoLock: new(sync.RWMutex),
	}
	service.initAppKeyInfo()
	return service
}
func GetAuthAppKeyService() *authAppKeyService {
	if authAppKey == nil {
		authAppKey = newAuthAppKeyService()
	}
	return authAppKey
}

//初始化加载配置,当前为硬编码
func (p *authAppKeyService) initAppKeyInfo() {
	p.appKeyInfo["ff983cecc76d406a8b1c7c3c1b7cd172"] = newAppKeyItem("fingertip android", "credit.app.qkier.com")
	p.appKeyInfo["cc0dec8c07444e9dadb75d41218c4fe4"] = newAppKeyItem("fingertip ios", "credit.app.qkier.com")
	p.appKeyInfo["067d2589ad6141799d935fb56495010c"] = newAppKeyItem("h5", "7fb5e9cfa0184773be03006fc6064645")
	p.appKeyInfo["271d8700b1074a9582a8cc4cdff94c5f"] = newAppKeyItem("qingke android", "9c9babfb8a3f4bb48d4ea651368585f2")
	p.appKeyInfo["0272050d21d641e2b6f188fde6e48476"] = newAppKeyItem("qingke ios", "4a25a6e2719648789640f9254dd862a9")
	p.appKeyInfo["84c0fa9de5301fe65fffd53bd6537ad7"] = newAppKeyItem("zhijian partner ios", "5384d31da7cb32d583b05268f21380d5")
	p.appKeyInfo["84c0fa9de5301fe65fffd53bd6537ad7"] = newAppKeyItem("zhijian partner android", "salt：c48053ab51da586cc18a6d7e937c5201")
}

//验证appkey是否有效
func (p *authAppKeyService) ValidateAppKey(appkey string) bool {
	_, ok := p.GetAppKeyItem(appkey)
	return ok
}

//根据appkey获取appkey的基本信息
func (p *authAppKeyService) GetAppKeyItem(appkey string) (*AppKeyItem, bool) {
	item, ok := p.appKeyInfo[appkey]
	return item, ok
}

//获取appkey对应的私钥
func (p *authAppKeyService) GetSecretKey(appkey string) (string, bool) {
	item, ok := p.GetAppKeyItem(appkey)
	if !ok {
		return "", false
	}
	return item.SecretKey, true
}
